var express = require('express');
var router = express.Router();
var { waterfall } = require("async");
var { conn, isError, dateFormat } = require("../utils");
var { ObjectID } = require("mongodb");

router.post('/works', function (req, res, next) {
  var body = req.body;
  conn((err, db) => {
    isError(err, res, db);
    var ky_works_info = db.collection("ky_works_info");
    waterfall([
      (cb) => {
        ky_works_info.findOne({ vid: body.vid, state: 1 }, {}, (err, result) => {
          isError(err, res, db);
          if (result) {
            body = result;
            cb(null, true)
          } else {
            cb(null, false);
          }
        })
      },
      (arg, cb) => {
        if (arg) {
          cb(null, body)
        } else {
          ky_works_info.insert(body, (err, result) => {
            isError(err, res, db);
            cb(null, body)
          })
        }
      }
    ], (err, result) => {
      isError(err, res, db);
      res.json(result);
      db.close();
    })
  })
});

router.post('/workLike', function (req, res, next) {
  var body = req.body;
  conn((err, db) => {
    isError(err, res, db);
    var ky_works_info = db.collection("ky_works_info");
    ky_works_info.find({ vid: { $in: body.ulike }, state: 1 }, {}).toArray((err, result) => {
      isError(err, res, db);
      res.json(result)
      db.close();
    })
  })
});

router.post("/works-like", (req, res) => {
  var body = req.body;
  conn((err, db) => {
    isError(err, res, db);
    var ky_works_info = db.collection("ky_works_info");
    ky_works_info.updateOne({ vid: body.vid, state: 1 }, { $addToSet: { vlike: body.vlike } }, (err, result) => {
      isError(err, res, db);
      if (result) {
        res.json({
          msg: "喜欢+1成功",
          code: 1
        });
      } else {
        res.json({
          msg: "喜欢+1失败",
          code: 0
        });
      }
      db.close();
    });
  })
})

router.post("/remove-works-like", (req, res) => {
  var body = req.body;
  conn((err, db) => {
    isError(err, res, db);
    var ky_works_info = db.collection("ky_works_info");
    ky_works_info.updateOne({ vid: body.vid, state: 1 }, { $pull: { vlike: body.vlike } }, (err, result) => {
      isError(err, res, db);
      if (result) {
        res.json({
          msg: "喜欢-1成功",
          code: 1
        });
      } else {
        res.json({
          msg: "喜欢-1失败",
          code: 0
        });
      }
      db.close();
    });
  })
})

router.post("/addComment", (req, res) => {
  var body = req.body;
  body.datetime = dateFormat(new Date(), "yyyy-mm-dd hh:SS:ss");
  conn((err, db) => {
    isError(err, res, db);
    var ky_comment_info = db.collection("ky_comment_info");
    ky_comment_info.insert(body, (err, result) => {
      isError(err, res, db);
      if (result) {
        res.json({
          msg: "评论添加成功",
          code: 1
        });
      } else {
        res.json({
          msg: "评论添加失败",
          code: 0
        });
      }
      db.close();
    });
  })
})

router.get("/commentList", (req, res) => {
  let vid = req.query.vid;
  conn((err, db) => {
    isError(err, res, db);
    var ky_comment_info = db.collection("ky_comment_info");
    ky_comment_info.find({ state: 1, vid: Number(vid) }, {}).sort({ _id: -1 }).toArray((err, result) => {
      isError(err, res, db);
      if (result) {
        res.json(result);
      } else {
        res.json({
          msg: "查询评论失败",
          code: 0
        });
      }
      db.close();
    });
  })
})

router.post("/addReply", (req, res) => {
  var body = req.body;
  body.datetime = dateFormat(new Date(), "yyyy-mm-dd hh:SS:ss");
  conn((err, db) => {
    isError(err, res, db);
    var ky_comment_info = db.collection("ky_comment_info");
    waterfall([
      (cb) => {
        ky_comment_info.insert(body, (err, result) => {
          isError(err, res, db);
          if (result) {
            cb(null, result.insertedIds[0])
          } else {
            cb(null, false)
          }
        });
      },
      (arg, cb) => {
        if (arg) {
          ky_comment_info.updateOne({ _id: ObjectID(body.replyId), state: 1 }, { $addToSet: { replyList: arg } }, (err, result) => {
            isError(err, res, db);
            if (result) {
              console.log(arg)
              console.log(body.replyId)
              cb(null, { msg: "回复成功", code: 1 })
            } else {
              cb(null, { msg: "回复失败", code: 0 })
            }
          });
        } else {
          cb(null, { msg: "回复失败", code: 0 })
        }
      }
    ], (err, result) => {
      isError(err, res, db);
      res.json(result)
      db.close();
    })
  })
})

router.post("/author", (req, res) => {
  var body = req.body;
  conn((err, db) => {
    isError(err, res, db);
    var ky_author_info = db.collection("ky_author_info");
    waterfall([
      (cb) => {
        ky_author_info.findOne({ vid: body.vid }, {}, (err, result) => {
          isError(err, res, db);
          if (result) {
            cb(null, true)
          } else {
            cb(null, false)
          }
        });
      },
      (arg, cb) => {
        if (arg) {
          cb(null, { msg: "关注数据已存在", code: 0 })
        } else {
          ky_author_info.insert(body, (err, result) => {
            isError(err, res, db);
            if (result) {
              cb(null, {
                msg: "关注数据添加成功",
                code: 1
              })
            } else {
              cb(null, {
                msg: "关注数据添加失败",
                code: 0
              })
            }
          });
        }
      }
    ], (err, result) => {
      isError(err, res, db);
      res.json(result)
      db.close();
    })
  })
})

router.post("/pgcList", (req, res) => {
  var body = req.body;
  conn((err, db) => {
    isError(err, res, db);
    var ky_pgc_info = db.collection("ky_pgc_info");
    waterfall([
      (cb) => {
        ky_pgc_info.findOne({ vid: body.vid }, {}, (err, result) => {
          isError(err, res, db);
          if (result) {
            cb(null, true)
          } else {
            cb(null, false)
          }
        });
      },
      (arg, cb) => {
        if (arg) {
          cb(null, { msg: "关注数据已存在", code: 0 })
        } else {
          ky_pgc_info.insert(body, (err, result) => {
            isError(err, res, db);
            if (result) {
              cb(null, {
                msg: "关注数据添加成功",
                code: 1
              })
            } else {
              cb(null, {
                msg: "关注数据添加失败",
                code: 0
              })
            }
          });
        }
      }
    ], (err, result) => {
      isError(err, res, db);
      res.json(result)
      db.close();
    })
  })
})

router.post("/theme", (req, res) => {
  var body = req.body;
  conn((err, db) => {
    isError(err, res, db);
    var ky_theme_info = db.collection("ky_theme_info");
    waterfall([
      (cb) => {
        ky_theme_info.findOne({ vid: body.vid }, {}, (err, result) => {
          isError(err, res, db);
          if (result) {
            cb(null, true)
          } else {
            cb(null, false)
          }
        });
      },
      (arg, cb) => {
        if (arg) {
          cb(null, { msg: "关注数据已存在", code: 0 })
        } else {
          ky_theme_info.insert(body, (err, result) => {
            isError(err, res, db);
            if (result) {
              cb(null, {
                msg: "关注数据添加成功",
                code: 1
              })
            } else {
              cb(null, {
                msg: "关注数据添加失败",
                code: 0
              })
            }
          });
        }
      }
    ], (err, result) => {
      isError(err, res, db);
      res.json(result)
      db.close();
    })
  })
})

router.post("/videos", (req, res) => {
  var body = req.body;
  conn((err, db) => {
    isError(err, res, db);
    var ky_videos_info = db.collection("ky_videos_info");
    waterfall([
      (cb) => {
        ky_videos_info.findOne({ vid: body.vid }, {}, (err, result) => {
          isError(err, res, db);
          if (result) {
            cb(null, true)
          } else {
            cb(null, false)
          }
        });
      },
      (arg, cb) => {
        if (arg) {
          cb(null, { msg: "关注数据已存在", code: 0 })
        } else {
          ky_videos_info.insert(body, (err, result) => {
            isError(err, res, db);
            if (result) {
              cb(null, {
                msg: "关注数据添加成功",
                code: 1
              })
            } else {
              cb(null, {
                msg: "关注数据添加失败",
                code: 0
              })
            }
          });
        }
      }
    ], (err, result) => {
      isError(err, res, db);
      res.json(result)
      db.close();
    })
  })
})

router.post("/getAttPgc", (req, res) => {
  var body = req.body;
  conn((err, db) => {
    isError(err, res, db);
    var ky_pgc_info = db.collection("ky_pgc_info");
    ky_pgc_info.find({ vid: { $in: body.uatt_pgc } }, {}).toArray((err, result) => {
      isError(err, res, db);
      res.json(result)
      db.close();
    });
  })
})

router.post("/getAuthor", (req, res) => {
  var body = req.body;
  conn((err, db) => {
    isError(err, res, db);
    var ky_author_info = db.collection("ky_author_info");
    ky_author_info.find({ vid: { $in: body.uatt_pgc } }, {}).toArray((err, result) => {
      isError(err, res, db);
      res.json(result)
      db.close();
    });
  })
})

router.post("/getTheme", (req, res) => {
  var body = req.body;
  conn((err, db) => {
    isError(err, res, db);
    var ky_theme_info = db.collection("ky_theme_info");
    ky_theme_info.find({ vid: { $in: body.uatt_theme } }, {}).toArray((err, result) => {
      isError(err, res, db);
      res.json(result)
      db.close();
    });
  })
})

router.post("/getVideos", (req, res) => {
  var body = req.body;
  conn((err, db) => {
    isError(err, res, db);
    var ky_videos_info = db.collection("ky_videos_info");
    ky_videos_info.find({ vid: { $in: body.uatt_theme } }, {}).toArray((err, result) => {
      isError(err, res, db);
      res.json(result)
      db.close();
    });
  })
})

router.post("/getUserComment", (req, res) => {
  var body = req.body;
  conn((err, db) => {
    isError(err, res, db);
    var ky_comment_info = db.collection("ky_comment_info");
    ky_comment_info.find({ uid: body.uid, state: 1 }, {}).toArray((err, result) => {
      isError(err, res, db);
      res.json(result)
      db.close();
    });
  })
})

router.post("/emptyLike", (req, res) => {
  var body = req.body;
  conn((err, db) => {
    isError(err, res, db);
    var ky_user_info = db.collection("ky_user_info");
    ky_user_info.updateOne({ _id: ObjectID(body.uid), state: 1 }, { $set: { ulike: [] } }, (err, result) => {
      isError(err, res, db);
      if (result) {
        res.json({
          msg: "清空成功",
          code: 1
        })
      } else {
        res.json({
          msg: "清空失败",
          code: 0
        })
      }
      db.close();
    });
  })
})

router.post("/emptyWorksLike", (req, res) => {
  var body = req.body;
  conn((err, db) => {
    isError(err, res, db);
    var ky_works_info = db.collection("ky_works_info");
    ky_works_info.updateMany({ state: 1, vlike: body.uid }, { '$pull': { vlike: body.uid } }, (err, result) => {
      isError(err, res, db);
      if (result) {
        res.json({
          msg: "清空成功",
          code: 1
        })
      } else {
        res.json({
          msg: "清空失败",
          code: 0
        })
      }
      db.close();
    });
  })
})

module.exports = router;
